package com.smartwasser.yunhtgl.apphtgl.asps;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.listener.DialogUIItemListener;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.activity.ImageLookActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat5Bean;
import com.smartwasser.yunhtgl.apphtgl.function.OfficeReader;
import com.smartwasser.yunhtgl.apphtgl.photo.ImagePagerActivity;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPNormal11PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPNormal1Presenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPNormal1PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.utils.DialogUI;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal11View;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal1View;
import com.smartwasser.yunhtgl.apphtgl.widget.QianzigaizhangView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.dou361.dialogui.DialogUIUtils.showAlertHorizontal;
import static com.dou361.dialogui.DialogUIUtils.showBottomSheetAndCancel;
import static com.smartwasser.yunhtgl.apphtgl.base.BaseConstant.successSPGreat1SH;
import static com.smartwasser.yunhtgl.apphtgl.network.Retrofits.base;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.isBCVisible;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getPath;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getProcurementMode;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getQicaoType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getSZType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getType;

/**
 * 待完成合同详情接口
 * 个人未处理
 * 一般合同
 * 归档审核
 */
public class SPNormal11Activity extends BaseActivity implements SPNormal11View {
    private SPNormal1Presenter sp1NormalPresenter = new SPNormal11PresenterImpl(this);
    private SPGreat5Bean type1Sp1NormalBean;
    private Dialog loadingHorizontal;
    private String fl_id;
    private FlMessageBean mflMessageBean;
    private String id;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.sp1_normal_scroll)
    ScrollView sp1_normal_scroll;
    @BindView(R.id.sp1_normal_name)
    TextView sp1_normal_name;//合同名称
    @BindView(R.id.sp1_normal_code)
    TextView sp1_normal_code;//合同编号
    @BindView(R.id.sp1_normal_type)
    TextView sp1_normal_type;//集采方式
    @BindView(R.id.sp1_normal_jia)
    TextView sp1_normal_jia;//甲方
    @BindView(R.id.sp1_normal_yi)
    TextView sp1_normal_yi;//乙方
    @BindView(R.id.sp1_normal_qt)
    TextView sp1_normal_qt;//其他方
    @BindView(R.id.sp1_normal_main)
    TextView sp1_normal_main;//合同主要内容
    @BindView(R.id.sp1_normal_bd)
    TextView sp1_normal_bd;//合同标的
    @BindView(R.id.sp1_normal_money)
    TextView sp1_normal_money;//金额(万)
    @BindView(R.id.sp1_normal_startTime)
    TextView sp1_normal_startTime;//执行期限
    @BindView(R.id.sp1_normal_qdTime)
    TextView sp1_normal_qdTime;//签订日期
    @BindView(R.id.sp1_great_fen)
    TextView sp1_great_fen;//合同份数
    @BindView(R.id.sp1_great_sq)
    TextView sp1_great_sq;//合同申请部室
    @BindView(R.id.sp1_great_zgy)
    TextView sp1_great_zgy;//合同专管员
    @BindView(R.id.sp1_great_creat_time)
    TextView sp1_great_creat_time;//合同申请时间
    @BindView(R.id.sp1_great_fzr)
    TextView sp1_great_fzr;//部室负责人
    @BindView(R.id.sp1_great_yj)
    TextView sp1_great_yj;//法律意见书
    @BindView(R.id.sp1_great_sz)
    TextView sp1_great_sz;//收支情况
    @BindView(R.id.sp1_great_lx)
    TextView sp1_great_lx;//合同类型
    @BindView(R.id.sp1_great_qz)
    TextView sp1_great_qz;//签字盖章
    @BindView(R.id.sp1_great_qc)
    TextView sp1_great_qc;//起草
    @BindView(R.id.sp1_great_zw)
    TextView sp1_great_zw;//正文
    @BindView(R.id.sp1_great_bz)
    TextView sp1_great_bz;
    @BindView(R.id.sp1_great_feed)
    EditText sp1_great_feed;
    @BindView(R.id.sp1_great_allow)
    TextView sp1_great_allow;//通不通过
    @BindView(R.id.sp1_great_check)
    CheckBox sp1_great_check;//按钮
    @BindView(R.id.sp1_great_fl_all)
    LinearLayout sp1_great_fl_all;//法律意见书可不可见

    @BindView(R.id.great_item_yy_time)
    TextView great_item_yy_time;//运营
    @BindView(R.id.great_item_yy_main)
    TextView great_item_yy_main;
    @BindView(R.id.great_item_yy_qz)
    TextView great_item_yy_qz;
    @BindView(R.id.great_item_fw_time)
    TextView great_item_fw_time;//法务
    @BindView(R.id.great_item_fw_main)
    TextView great_item_fw_main;
    @BindView(R.id.great_item_fw_qz)
    TextView great_item_fw_qz;
    private ArrayList<String> imageUrls = new ArrayList<>();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sp_normal1);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("一般合同审核");
        sp1_normal_scroll.setVisibility(View.INVISIBLE);
        id = getIntent().getStringExtra("id");
        sp1_great_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean boom) {
                if (boom) {
                    sp1_great_allow.setText("不通过");
                    sp1_great_feed.setText("");
                } else {
                    sp1_great_allow.setText("通过");
                    sp1_great_feed.setText("通过");
                }
            }
        });
    }

    @Override
    public void initData() {
        loadingHorizontal = showLoadingHorizontal(this, "加载中...").show();
        try {
            sp1NormalPresenter.getType1Sp1Normal(getStringData(this, "JSESSIONID", ""), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, R.id.sp1_normal_sh, R.id.sp1_great_fl_chose, R.id.sp1_great_zw_all})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sp1_great_zw_all:
                if (null != type1Sp1NormalBean.getBuContractBaseInfo()) {
                    String path = type1Sp1NormalBean.getBuContractBaseInfo().getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
            case R.id.sp1_normal_sh:
                if (sp1_great_feed.getText().toString().trim().length() > 0) {
                    Logger.e("R.id.sp1_great_sh");
                    commitSH();
                } else {
                    showResult("请输入审核意见");
                }
                break;
            case R.id.sp1_great_fl_chose:
                if (null != mflMessageBean && mflMessageBean.getFlyjsList().size() > 0) {
                    commitShow(mflMessageBean);
                } else {
                    loadingHorizontal = showLoadingHorizontal(this, "请稍等...").show();
                    sp1NormalPresenter.getFlMessage(getStringData(this, "JSESSIONID", ""));
                }
                break;
        }
    }

    @Override
    public void success(SPGreat5Bean tb) {
        if (null != tb && null != tb.getBuContractBaseInfo()) {
            this.type1Sp1NormalBean = tb;
            sp1_normal_code.setText(tb.getBuContractBaseInfo().getCode());
            sp1_normal_name.setText(tb.getBuContractBaseInfo().getName());
            sp1_normal_type.setText(getProcurementMode(tb.getBuContractBaseInfo().getProcurementMode()));
            sp1_great_lx.setText(getType(tb.getBuContractBaseInfo().getType()));
            sp1_great_qc.setText(getQicaoType(tb.getBuContractBaseInfo().getQicaoType()));
            sp1_great_zw.setText(getPath(tb.getBuContractBaseInfo().getPath()));
            sp1_great_sz.setText(getSZType(tb.getBuContractBaseInfo().getShouzhiType()));
            if (null != tb.getBuContractBaseInfo().getBuMajorContractSignInfoList() && tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().size() > 0) {
                sp1_normal_jia.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getJiaFangName());
                sp1_normal_yi.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getYiFangName());
                sp1_normal_qt.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getOtherFangName());
                sp1_normal_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMainContent());
                sp1_normal_bd.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractBiaodi());
                sp1_normal_money.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMoney());
                sp1_normal_startTime.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeStart());
                sp1_great_fen.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractCount() + "份");
                sp1_great_sq.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDept().getName());
                sp1_great_zgy.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_great_creat_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDate());
                sp1_great_fzr.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractDwOfficer().getName());
                sp1_great_bz.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getRemarks());

                great_item_yy_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveCheckDate());
                great_item_yy_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveUpdateOpinion());
                great_item_yy_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveName().getName());
                great_item_fw_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptCheckDate());
                great_item_fw_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptUpdateOpinion());
                great_item_fw_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptOfficerName().getName());
            }
            if (isBCVisible(tb.getBuContractBaseInfo().getGdAct().getTaskDefKey())) {
                sp1_great_fl_all.setVisibility(View.VISIBLE);
                String qzht = tb.getBuContractBaseInfo().getQianziContract();
                if (!qzht.isEmpty() && qzht.length() > 0) {
                    final String[] qiht=qzht.split("\\|");
                    for(int i=1;i<qiht.length;i++){
                        imageUrls.add(base+qiht[i]);
                        QianzigaizhangView qianzigaizhangView=new QianzigaizhangView(SPNormal11Activity.this,null);
                        String[] qzgz_array = qiht[i].split("/");
                        qianzigaizhangView.setText(qzgz_array[qzgz_array.length-1]);
                        if (i==1)
                            qianzigaizhangView.setVisibilityTitle();
                        sp1_great_fl_all.addView(qianzigaizhangView);
                        final int finalI = i;
                        qianzigaizhangView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (null != type1Sp1NormalBean.getBuContractBaseInfo()) {
                                    String path = qiht[finalI];
                                    if (!path.isEmpty() && path.length() > 0) {
                                        if(path.contains(".JPG")||path.contains(".PNG")||path.contains(".jpg")||path.contains(".png")){
//                                            startActivity(ImageLookActivity.class, "url", path);
                                            imageBrower(finalI-1);
                                        }else{
                                            startActivity(OfficeReader.class, "url", path);
                                        }
                                    } else {
                                        showResult("暂无数据");
                                    }
                                } else {
                                    showResult("暂无数据");
                                }
                            }
                        });
                    }
                }
            } else {
                sp1_great_fl_all.setVisibility(View.GONE);
            }
        }
        sp1_normal_scroll.setVisibility(View.VISIBLE);
        dismiss(loadingHorizontal);
    }
    private void imageBrower(int position) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, imageUrls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        startActivity(intent);
    }
    @Override
    public void successGetFlMessage(FlMessageBean flMessageBean) {
        this.mflMessageBean = flMessageBean;
        dismiss(loadingHorizontal);
        commitShow(flMessageBean);
    }

    @Override
    public void successCommitSH(BaseBean baseBean) {
        dismiss(loadingHorizontal);
        showResult("审核成功");
        setResult(successSPGreat1SH);
        finish();
    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
    }

    public void commitShow(final FlMessageBean flMessageBean) {
        final List<String> strings = new ArrayList<>();
        for (int i = 0; i < flMessageBean.getFlyjsList().size(); i++) {
            strings.add(flMessageBean.getFlyjsList().get(i).getName());
        }
        showBottomSheetAndCancel(this, strings, "取消", new DialogUIItemListener() {
            @Override
            public void onItemClick(CharSequence text, int position) {
                sp1_great_yj.setText(flMessageBean.getFlyjsList().get(position).getName());
                fl_id = flMessageBean.getFlyjsList().get(position).getId();
            }
        }).show();
    }

    public void commitSH() {
        final String flag;
        final String comment = sp1_great_feed.getText().toString().trim();
        final String shouzhiType = type1Sp1NormalBean.getBuContractBaseInfo().getShouzhiType();
        final String type = type1Sp1NormalBean.getBuContractBaseInfo().getType();
        if (sp1_great_check.isChecked()) {
            flag = "1";
        } else {
            flag = "0";
        }
        Logger.e("JSESSIONID = " + getStringData(SPNormal11Activity.this, "JSESSIONID", ""));
        Logger.e("id = " + id);
        Logger.e("flag = " + flag);
        Logger.e("comment " + comment);
        Logger.e("shouzhiType " + shouzhiType);
        Logger.e("type = " + type);
        showAlertHorizontal(this, "审核提示", "确定要审核吗？", new DialogUI() {
            @Override
            public void onPositive() {
                try {
                    if (null != fl_id && !fl_id.isEmpty() && fl_id.length() > 0) {
                        sp1NormalPresenter.getCommitSH(getStringData(SPNormal11Activity.this, "JSESSIONID", ""), id, flag, comment, shouzhiType, type, fl_id);
                    } else {
                        sp1NormalPresenter.getCommitSH(getStringData(SPNormal11Activity.this, "JSESSIONID", ""), id, flag, comment, shouzhiType, type, null);
                    }
                } catch (Exception e) {
                    Logger.e("请求网络大法启动失败");
                }
            }
        }).show();
    }
}